package com.moco.launcher.fragment;

import java.util.ArrayList;
import java.util.List;

import com.moco.launcher.R;
import com.moco.launcher.activity.AboutActivity;
import com.moco.launcher.activity.AlarmListActivity;
import com.moco.launcher.activity.GestureStatues;
import com.moco.launcher.activity.NewWifiSettingsActivity;
import com.moco.launcher.activity.RingActivity;
import com.moco.launcher.activity.ShutDownActivity;
import com.moco.launcher.activity.SleepDelayActivity;
import com.moco.launcher.activity.UpateActivity;
import com.moco.launcher.activity.UpdateMsgActivity;
import com.moco.launcher.activity.showDownAppActivity;
import com.moco.launcher.adapter.SettingListAdapter;
import com.moco.launcher.bean.MOCO_CONSTANTS;
import com.moco.launcher.bean.MocoItemInfo;
import com.moco.launcher.bean.PickItemInfo;
import com.oazon.common.util.Utils;
import com.moco.launcher.view.CategoryPickerView;
import com.moco.launcher.view.PickerView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class SettingFragment extends BaseFragment {

	private static final String TAG = "SettingFragment";

	List<PickItemInfo> data = new ArrayList<PickItemInfo>();
	ListView mListView = null;
	SettingListAdapter mAapter = null;
	PickerView mPickerView = null;
	private int ACTIVITY_RESULT_FOR_WIFISETTING=123;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		PickItemInfo item = new PickItemInfo();
		item.setText(this.getActivity().getResources()
				.getString(R.string.setting_update));
		item.setLeftDarkIconId(R.drawable.update_dark);
		item.setLeftLightIconId(R.drawable.update_light);
		item.setId("0");
		data.add(item);

		item = new PickItemInfo();
		item.setText(this.getActivity().getResources()
				.getString(R.string.setting_wifi));
		item.setLeftDarkIconId(R.drawable.wifi_dark);
		item.setLeftLightIconId(R.drawable.wifi_light);
		item.setId("1");
		data.add(item);

		item = new PickItemInfo();
		item.setText(this.getActivity().getResources()
				.getString(R.string.setting_shutdown));
		item.setLeftDarkIconId(R.drawable.shutdown_dark);
		item.setLeftLightIconId(R.drawable.shutdown_light);
		item.setId("2");
		data.add(item);
		
		item = new PickItemInfo();
		item.setText(this.getActivity().getResources()
				.getString(R.string.about));
		item.setLeftDarkIconId(R.drawable.about_dark);
		item.setLeftLightIconId(R.drawable.about_light);
		item.setId("3");
		data.add(item);
		
		item = new PickItemInfo();
		item.setText(this.getActivity().getResources()
				.getString(R.string.gestures));
		item.setLeftDarkIconId(R.drawable.about_dark);
		item.setLeftLightIconId(R.drawable.about_light);
		item.setId("4");
		data.add(item);
		item = new PickItemInfo();
		item.setText(this.getActivity().getResources()
				.getString(R.string.app_down));
		item.setLeftDarkIconId(R.drawable.down_app_ico_miss);
		item.setLeftLightIconId(R.drawable.down_app_ico_normal);
		item.setId("5");
		data.add(item);

		mAapter = new SettingListAdapter(this.getContext(),
				R.layout.item_list_image, data);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.setting_layout, container,
				false);
		mPickerView = (PickerView) rootView.findViewById(R.id.setting_list);
		mPickerView.setData(data);
		mPickerView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mPickerView.isSettingClickUp == true)
					return;
				
				if(mPickerView.settingClickY<mPickerView.getHeight()/3){
					settingPickerClick(v,-1);
				}else if(mPickerView.settingClickY>(mPickerView.getHeight()/3)*2){
					settingPickerClick(v, 1);
				}else if(mPickerView.settingClickY>mPickerView.getHeight()/3||
						mPickerView.settingClickY<(mPickerView.getHeight()/3)*2){
					settingPickerClick(v, 0);

			}
				}
				
				
		
		});
		return rootView;
	}
	public void settingPickerClick(View v,int i){
		
		Log.e(TAG, "ONCLICK.......");
		PickerView view = (PickerView) v;
		String id = view.getCurrentSelectId();
        if (i==-1){
        	if (id.equals("0")) {		
        		 startActivity(showDownAppActivity.class);
        		 		
    		} else if (id.equals("1")) {
    			startActivity(UpdateMsgActivity.class);   			
    		} else if (id.equals("2")) {
    			startWifiSettingView(); 		
    		} else if (id.equals("3")) {
    			startActivity(ShutDownActivity.class);    		
    		}
    		 else if (id.equals("4")) {
    			 startActivity(AboutActivity.class);    			
    			}
    		 else if(id.equals("5")){
    			 startActivity(GestureStatues.class); 
    		 }
        	return;
        }
        if(i==0){
        	if (id.equals("0")) {
        		startActivity(UpdateMsgActivity.class);
    		} else if (id.equals("1")) {
    			startWifiSettingView(); 
    		} else if (id.equals("2")) {
    			startActivity(ShutDownActivity.class);
    		} else if (id.equals("3")) {
    			startActivity(AboutActivity.class);
    		}
    		 else if (id.equals("4")) {
    			 startActivity(GestureStatues.class);
    			}
    		 else if(id.equals("5")){
    			 startActivity(showDownAppActivity.class);
    		 }
        	return;
        }
        if(i==1){
        	if (id.equals("0")) {
        		startWifiSettingView();
        	} else if (id.equals("1")) {
        		startActivity(ShutDownActivity.class);
       		} else if (id.equals("2")) {
    			startActivity(AboutActivity.class);    			
    		} else if (id.equals("3")) {
    			startActivity(GestureStatues.class);
    		}
    		 else if (id.equals("4")) {
    			 startActivity(showDownAppActivity.class);
    			}
    		 else if(id.equals("5")){
    			 
    			 startActivity(UpdateMsgActivity.class);
    		 }
        	return;
        
        }
	
	
	}
	private void startWifiSettingView(){
		Intent intent = new Intent(SettingFragment.this
				.getContext(), NewWifiSettingsActivity.class);
	    startActivityForResult(intent, ACTIVITY_RESULT_FOR_WIFISETTING);
	}
	public void startActivity(Class<?> Activity ){
		Intent intent = new Intent(SettingFragment.this
				.getContext(), Activity);
		startActivity(intent);
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		Log.d(TAG, "setUserVisibleHint=="+isVisibleToUser);
		if(isVisibleToUser){
			if(mPickerView !=null){
				mPickerView.setClickable(true);
			}
		}
	}
	
}
